﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using System.ServiceModel;
using MCMLXVII.BackEnd.Services.Common.Messages;
#endregion

namespace MCMLXVII.Tools.OrganizationChart
{
    public class OrganizationTreeNode
    {
        private msgOrganizationNodeInfo _baseOrganizationNodeInfo;
        private int _NodeIndex;
        private string[] _Permissions;
        private bool _PermissionsInitialized = false;
        private msgOrganizationTreeNodeMember[]  _Members;
        private bool _MembersInitialized = false;        


        public OrganizationTreeNode(msgOrganizationNodeInfo BaseInfo, int TreeNodeIndex)
        {
            _baseOrganizationNodeInfo = BaseInfo;
            _NodeIndex = TreeNodeIndex;            
        }

        public msgOrganizationNodeInfo BaseOrganizationNodeInfo
        {
            get { return _baseOrganizationNodeInfo; }
            set { _baseOrganizationNodeInfo = value; }
        }
        public int NodeIndex
        {
            get { return _NodeIndex; }
            set { _NodeIndex = value; }
        }
        public bool ArePermissionsInitialized()
        {
            return _PermissionsInitialized;
        }
        public bool CanDelete()
        {
            if (_PermissionsInitialized)
            {
                foreach (string x in Permissions)
                {
                    if (x.IndexOf('F') > -1 || x.IndexOf('D') > -1)
                        return true;
                }
                return false;
            }
            else
                return false;
        }
        public bool CanUpdate()
        {
            if (_PermissionsInitialized)
            {
                foreach (string x in Permissions)
                {
                    if (x.IndexOf('F') > -1 || x.IndexOf('U') > -1)
                        return true;
                }
                return false;
            }
            else
                return false;
        }
        public bool CanAudit()
        {
            if (_PermissionsInitialized)
            {
                foreach (string x in Permissions)
                {
                    if (x.IndexOf('A') > -1)
                        return true;
                }
                return false;
            }
            else
                return false;
        }
        public bool CanManageSecurity()
        {
            if (_PermissionsInitialized)
            {
                foreach (string x in Permissions)
                {
                    if (x.IndexOf('S') > -1)
                        return true;
                }
                return false;
            }
            else
                return false;
        }
        public string[] Permissions
        {
            get { return _Permissions; }
            set 
            {
                _PermissionsInitialized = true;
                _Permissions = value;
            }
        }

        public void ClearMembers()
        {
            _Members = new msgOrganizationTreeNodeMember[0];
            _MembersInitialized = true;
        }
        public bool AreMembersInitialized()
        {
            return _MembersInitialized;
        }
        public void AddMember(msgOrganizationTreeNodeMember item)
        {
            if (_MembersInitialized == false)
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional, Properties.Resources.NotInitialized));
            else
            {
                if (_Members != null)
                {
                    msgOrganizationTreeNodeMember[] tmp = new msgOrganizationTreeNodeMember[_Members.Length + 1];
                    Array.Copy(_Members, tmp, _Members.Length);
                    tmp[tmp.Length - 1] = item;
                    _Members = tmp;
                }
                else
                {
                    _Members = new msgOrganizationTreeNodeMember[] { item };
                }
            }
        }
        public void UpdateMember(int MemberID, string Description)
        {
            if (_MembersInitialized == false)
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional, Properties.Resources.NotInitialized));
            else
            {
                if (_Members != null)
                {
                    foreach (msgOrganizationTreeNodeMember item in _Members)
                    {
                        if (item.MemberID == MemberID)
                            item.Description = Description;
                    }
                }
            }
        }

        public void RemoveMember(int MemberID)
        {
            if (_MembersInitialized == false)
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional, Properties.Resources.NotInitialized));
            else
            {
                if (_Members != null)
                {
                    for (int i = 0; i < _Members.Length; i++)
                    {
                        if (_Members[i].MemberID == MemberID)
                        {
                            msgOrganizationTreeNodeMember[] tmp = new msgOrganizationTreeNodeMember[_Members.Length - 1];
                            Array.Copy(_Members, tmp, i);
                            if (i < _Members.Length - 1)
                                Array.Copy(_Members, i + 1, tmp, i, _Members.Length - i - 1);
                            _Members = tmp;
                        }
                    }
                }
            }
        }

        public void ReInitMembers()
        {
            _MembersInitialized = false;
        }

        public msgOrganizationTreeNodeMember[] Members
        {
            get { return _Members; }
            set
            {
                _Members = value;
                if (value == null)
                    _Members = new msgOrganizationTreeNodeMember[0];
                _MembersInitialized = true;
                
            }
        }

        public void LoadEditorInfo(NodeEditorHelper NewInfo)
        {

            _baseOrganizationNodeInfo.Name = NewInfo.Name;
            _baseOrganizationNodeInfo.DirectoryConfigInfo = NewInfo.DirectoryServiceConfigInfo;
            int cant = 0;
            foreach (StringTranslations trans in NewInfo.NameTranslated.Collection)
            {
                if (trans.Translation != "")
                    cant++;
            }
            if (cant > 0)
            {
                NodeLanguageTranslations[] ArrTrans = new NodeLanguageTranslations[cant];
                cant = 0;
                foreach (StringTranslations trans in NewInfo.NameTranslated.Collection)
                {
                    if (trans.Translation != "")
                    {
                        ArrTrans[cant] = new NodeLanguageTranslations(trans.Language, trans.Translation);
                        cant++;
                    }
                }
                _baseOrganizationNodeInfo.Translations = ArrTrans;
            }
            else
            {
                _baseOrganizationNodeInfo.Translations = null;
            }
        }
    }
}
